import { _decorator, Component, Node, tween, UIOpacity, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('BCPanelBase')
export class BCPanelBase extends Component {
	@property(UIOpacity)
	protected m_cover: UIOpacity = null;

	@property(Node)
	protected m_viewNode: Node = null;

	protected m_isHideAni: boolean = false;	  // 是否隐藏打开与关闭的动效

	protected m_viewClosedCallback: ()=>void = null;
	set _viewClosedCallback(cb: () => void){
		this.m_viewClosedCallback = cb;
	}

	_resetView(){
		this.m_cover && (this.m_cover.opacity = 0);
		this.m_viewNode.scale = Vec3.ZERO;
	}

	_finishView() {
		this.m_cover && (this.m_cover.opacity = 160);
		this.m_viewNode.scale = Vec3.ONE;
	}

	_showView(option?: any) {
		this.m_isHideAni = option && !!option.isHideAni;
		if (this.m_isHideAni) {
			this._finishView()
			return
		}

		if (this.m_cover){
			tween(this.m_cover).to(0.1, { opacity: 160 }).call(() => {
				tween(this.m_viewNode).to(0.2, { scale: Vec3.ONE }, { easing: 'backOut' }).start();
			}).start();
		} else {
			tween(this.m_viewNode).to(0.2, { scale: Vec3.ONE }, { easing: 'backOut' }).start();
		}
	}

	async _closeView(): Promise<void>{
		return new Promise(res=>{
			if (this.m_isHideAni) {
				this._resetView();
				this.m_viewClosedCallback && this.m_viewClosedCallback();
				res();
				return
			}

			tween(this.m_viewNode).to(0.2, { scale: Vec3.ZERO }, { easing: 'backIn' }).call(() => {
				if (this.m_cover) {
					tween(this.m_cover).to(0.1, { opacity: 0 }).call(() => {
						this.m_viewClosedCallback && this.m_viewClosedCallback();
						res();
					}).start();
				} else {
					this.m_viewClosedCallback && this.m_viewClosedCallback();
					res();
				}
			}).start();
		});
	}

	onDestroy() {
		this.m_viewClosedCallback && this.m_viewClosedCallback();
	}
}

